package cn.wp.common.core.plugin.scheduler;

import cn.wp.common.core.exception.WpServiceException;
import cn.wp.common.core.plugin.fileserver.FileInfo;
import cn.wp.common.core.plugin.fileserver.FileServerProperties;
import cn.wp.common.core.plugin.fileserver.StorageStrategy;
import cn.wp.common.core.plugin.fileserver.strategy.DateStrategy;
import cn.wp.common.core.plugin.fileserver.strategy.TopicStrategy;
import cn.wp.common.core.util.ApplicationContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.util.ErrorHandler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 定时器服务配置
 * Created by chenyb on 2019/10/23
 */
@Configuration
@Slf4j
public class SchedulerConfiguration implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setTaskScheduler(taskScheduler());
    }

    /**
     * 调度管理器配置
     * @return
     */
    @Bean(destroyMethod = "shutdown")
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(150);
        scheduler.setThreadNamePrefix("wp-dispatch-");
        scheduler.setAwaitTerminationSeconds(600);
        scheduler.setErrorHandler(new ErrorHandler() {
            @Override
            public void handleError(Throwable throwable) {
                log.error("调度任务发生异常", throwable);
            }
        });
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        return scheduler;
    }

    /**
     * 调度容器
     * @return
     */
    @Bean
    SchedulerContainer schedulerContainer(){
        return new SchedulerContainer();
    }

}
